#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
    pid_t pid = fork();
    if(pid==0)
    {
        int cnt = 5;
        while(cnt--)
        {
            printf("我是一个子进程，pid:%d，ppid:%d\n", getpid(), getppid());
            sleep(1);
        }
        exit(4);
    }

    sleep(5);
    int status;
    pid_t rid = waitpid(pid, &status, 0);
    if(rid>0)
    {
        if(WIFEXITED(status))
        {
            printf("success wait，status:%d，exit code:%d, exit sign:%d\n", status, WEXITSTATUS(status), status&0x7F);
            //printf("success wait，status:%d，exit code:%d, exit sign:%d\n", status, (status>>8)&0xFF, status&0x7F)
        }   
    }

    //pid_t rid = wait(NULL);
    //if(rid>0)
    //{
    //    printf("success wait，rip:%d\n", rid);    
    //}

    return 0;
}


//int main()
//{
//    //printf("hello world\n");
//    for(int i=0; i<200; ++i)
//        printf("%d->%s\n", i, strerror(i));
//
//    FILE* pf = fopen("log.txt", "r");
//    if(pf == NULL) return errno;
//
//    return 0;
//}
